(function() {
  "use strict";

  /**
   * @ngdoc directive
   * @name webApp.directive:resource
   * @description
   * # resource
   */
  angular
    .module("webApp")
    //调用其他方法
    .run([
      "$templateCache",
      "dataService",
      function($templateCache, dataService) {
        var tplName = "views/directives.resource.html";
        dataService.tpl(tplName).then(function(html) {
          $templateCache.put(tplName, html);
        });
      }
    ])
    .directive("resource", [
      "dataService",
      function(dataService) {
        return {
          restrict: "E",
          // require: 'ngModel',
          scope: {
            chosemodel: "=",
            callback: "&"
          },
          template:
            "<a href='javascript:void(0);' class='sui-btn btn-success' ng-click='chose();'><i class='sui-icon icon-tb-add'></i>{{'Resources' | translate}}</a>",
          controller: [
            "$templateCache",
            "$rootScope",
            "$scope",
            "$compile",
            "$timeout",
            function($templateCache, $rootScope, $scope, $compile, $timeout) {
              $scope.choseapis = [];
              $scope.choseapi = {};

              $scope.chose = function() {
                if ($scope.chosemodel) {
                  _.map($scope.chosemodel, function(x, k) {
                    if (x) {
                      $scope.choseapis.push({
                        identity: x.identity,
                        name: x.name
                        // action: x.action,
                      });
                      $scope.choseapi[x.name] = x.action;
                    }
                  });
                  console.error(
                    "$scope.newitems",
                    $scope.choseapi,
                    $scope.items
                  );
                }
                $scope.pageData().then(showPop);
              };

              $scope.check = function(value, checked) {
                if (value && !checked) {
                  delete $scope.choseapi[value.name];
                }
              };

              $scope.allcheck = function(value, checked) {
                if (value && !checked) {
                  delete $scope.choseapi[value.name];
                }
                if (value && checked) {
                  $scope.choseapi[value.name] = value.action;
                }
              };

              function showPop() {
                $.alert({
                  backdrop: "static",
                  keyboard: true,
                  title:
                    '<form class="sui-form" style="margin:0px;"><span>{{"choice" | translate}}{{"Resources" | translate}}</span></form>',
                  body: $templateCache.get("views/directives.resource.html"),
                  okBtn: '{{"ok" | translate}}',
                  closeBtn: true,
                  transition: true,
                  width: "large",
                  height: "600",
                  show: function(e) {
                    $compile($(e.currentTarget).contents())($scope);
                    return true;
                  },
                  okHide: function(e) {
                    $timeout(function() {
                      $scope.fitems = [];
                      _.map($scope.choseapis, function(x) {
                        if (x) {
                          var k = x.name;
                          if ($scope.choseapi[k]) {
                            $scope.fitems = _.uniq($scope.fitems);
                            $scope.fitems.push({
                              action: $scope.choseapi[k],
                              identity: x.identity,
                              name: x.name
                            });
                          }
                        }
                      });
                      //q去除重复值f
                      if ($scope.fitems.length) {
                        for (var i = $scope.fitems.length - 1; i >= 0; i--) {
                          for (var j = i - 1; j >= 0; j--) {
                            if (
                              $scope.fitems[i] &&
                              $scope.fitems[j] &&
                              $scope.fitems[i].name == $scope.fitems[j].name
                            ) {
                              $scope.fitems.splice(j, 1);
                            }
                          }
                        }
                      }
                      $scope.callback({ permissions: $scope.fitems });
                    });
                  }
                });
              }

              //请求数据
              $scope.itemsTotal = 0;
              $scope.pageSize = 10;
              $scope.pageData = function(pageNo) {
                pageNo = pageNo ? pageNo : 1;
                var query = { page_no: pageNo, page_size: $scope.pageSize };
                if ($scope.identity) {
                  query.query = { "identity.icontains": $scope.identity };
                }

                return dataService.resourceGet(query).then(function(data) {
                  $scope.items = data.resources;
                  $scope.newitems = {};
                  _.map($scope.items, function(cd, k) {
                    $scope.newitems[cd.name] = {
                      identity: cd.identity,
                      name: cd.name
                      // action: cd.action,
                    };
                  });
                  $scope.itemsTotal = data.total_count;
                  console.error(
                    "$scope.newitems",
                    $scope.newitems,
                    $scope.items
                  );
                });
              };
            }
          ],
          link: function(scope, element, attrs, ctrl) {
            //监控变化

            ctrl.$render = function() {};
          }
        };
      }
    ])
    .run([
      "$templateCache",
      "dataService",
      function($templateCache, dataService) {
        var tplName = "views/directives.fujiaresource.html";
        dataService.tpl(tplName).then(function(html) {
          $templateCache.put(tplName, html);
        });
      }
    ])
    .directive("fujiaResource", [
      "dataService",
      function(dataService) {
        return {
          restrict: "E",
          // require: 'ngModel',
          scope: {
            chosemodel: "=",
            callback: "&"
          },
          template:
            "<a href='javascript:void(0);' class='sui-btn btn-success' ng-click='chose();'><i class='sui-icon icon-tb-add'></i>{{'Resources' | translate}}</a>",
          controller: [
            "$templateCache",
            "$rootScope",
            "$scope",
            "$compile",
            "$timeout",
            function($templateCache, $rootScope, $scope, $compile, $timeout) {
              $scope.chose = function() {
                $timeout(function() {
                  $scope.chosemodel = $scope.chosemodel
                    ? $scope.chosemodel
                    : [];
                  // console.error("$scope.chosemodel", $scope.chosemodel);
                });
                $scope.pageData().then(showPop);
              };

              function showPop() {
                $.alert({
                  backdrop: "static",
                  keyboard: true,
                  title:
                    '<form class="sui-form" style="margin:0px;"><span>{{"choice" | translate}}{{"Resources" | translate}}</span></form>',
                  body: $templateCache.get(
                    "views/directives.fujiaresource.html"
                  ),
                  okBtn: '{{"ok" | translate}}',
                  closeBtn: true,
                  transition: true,
                  width: "large",
                  height: "400",
                  show: function(e) {
                    $compile($(e.currentTarget).contents())($scope);
                    return true;
                  },
                  okHide: function(e) {
                    $timeout(function() {
                      if ($scope.callback) {
                        $scope.callback({ resoucres: $scope.chosemodel });
                      }
                    });
                  }
                });
              }

              //请求数据
              $scope.itemsTotal = 0;
              $scope.pageSize = 10;
              $scope.pageData = function(pageNo) {
                pageNo = pageNo ? pageNo : 1;
                var query = {
                  page_no: pageNo,
                  page_size: $scope.pageSize,
                  status: 1
                };
                if ($scope.controller) {
                  query.controller = $scope.controller;
                }
                return dataService
                  .phpResourcesGet(query, 5 * 60)
                  .then(function(data) {
                    $scope.items = data.resource_list;
                    $scope.itemsTotal = data.total_count;
                  });
              };
            }
          ],
          link: function(scope, element, attrs, ctrl) {
            //监控变化

            ctrl.$render = function() {};
          }
        };
      }
    ])
    .directive("resourceOne", [
      "dataService",
      function(dataService) {
        return {
          restrict: "E",
          // require: 'ngModel',
          scope: {
            chosemodel: "="
          },
          template:
            "<span>控制名称：{{controller}} <br/>方法名称：{{action}}</span>",
          controller: [
            "$templateCache",
            "$rootScope",
            "$scope",
            "$compile",
            "$timeout",
            function($templateCache, $rootScope, $scope, $compile, $timeout) {
              //请求数据
              $scope.pageData = function(pageNo) {
                var query = { pid: $scope.chosemodel };
                return dataService.phpResourcesGet(query).then(function(data) {
                  if (data.resource_list) {
                    $scope.controller = data.resource_list[0].controller;
                    $scope.action = data.resource_list[0].action;
                  }
                });
              };
              $scope.pageData();
            }
          ],
          link: function(scope, element, attrs, ctrl) {
            //监控变化

            ctrl.$render = function() {};
          }
        };
      }
    ]);
})();
